package com.wz.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wz.entity.Emp;
import com.wz.service.impl.EmpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.util.List;

@Controller
@RequestMapping("emp")
public class EmpController {
    @Autowired
    private EmpServiceImpl empService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("add")
    public String save1() {
        return "add";
    }

    @PostMapping("add2")
    public String save2(Emp emp, HttpServletRequest request) {
        empService.save(emp);
        request.getSession().setAttribute("pageInfo",getPageInfo());
        return "manger";
    }
    private PageInfo<Emp> getPageInfo(){
        PageHelper.startPage(1, 10);
        List<Emp> emps = empService.findEmp();
        PageInfo<Emp> pageInfo = new PageInfo<>(emps);//使用PageInfo包装类
        return pageInfo;

    }


    @RequestMapping("/delete/{empno}")
    public String delte(@PathVariable("empno") String empno, HttpServletRequest request) {
        empService.delete(empno);
        request.getSession().setAttribute("pageInfo",getPageInfo());
        return "manger";
    }

    @RequestMapping("/updatePage/{empno}")
    public String updatePage(@PathVariable("empno") String empno, Model model) {

        Emp emp = empService.get(empno);
        model.addAttribute("emp", emp);
        return "update";


    }

    @PostMapping("update")
    public String updateEmp(Emp emp, HttpServletRequest request) {
        Emp emp1 = empService.get(emp.getEmpno());
        System.out.println("被修改前的员工：" + emp1);
        empService.update(emp);
        request.getSession().setAttribute("pageInfo",getPageInfo());

        return "manger";
    }


    @PostMapping("/showTopone1")
    public String showTopone(String empno,Model model) {
           String value = stringRedisTemplate.opsForValue().get(empno);
        System.out.println(value);
        if (value == null) {
            System.out.println("=======================走mysql===========================");

            Emp emp = empService.get(empno);
            System.out.println(emp);
            if(emp == null||emp.equals("")){
                model.addAttribute("msg","不存在该员工！");
                return "seletEmp";
            }else {
                //将值拼出来
                StringBuffer sb = new StringBuffer();
                sb.append(emp.getEname()).append(",").append(emp.getJob())
                        .append(",").append(emp.getMgr())
                        .append(",").append(emp.getHiredata())
                        .append(",").append(emp.getSal())
                        .append(",").append(emp.getComm())
                        .append(",").append(emp.getDeptno());
                String value2 = sb.toString();


                stringRedisTemplate.opsForValue().set(empno, value2);

                stringRedisTemplate.expire(empno, Duration.ofMinutes(1L));


                model.addAttribute("empone", emp);
                return "showTopone";
            }
        } else {
            System.out.println("==================直接走redis,不走mysql=========================");
            String[] strings = value.split(",");

            Emp emp = new Emp(empno, strings[0], strings[1], strings[2], strings[3], strings[4], strings[5], strings[6]);
            model.addAttribute("empone", emp);
            return "showTopone";
        }
    }


}
